<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../themes/icon.css">
    <script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript" src="../jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="../jquery.serializeObject.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor-1.2.2.min.js"></script>
    <script type="text/javascript" src="../xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
<div id="new" class="easyui-window" draggable=false fit="true" title="新闻列表" icon="icon-help" style="width:500px;height:250px;padding:5px;background: #fafafa;">
    <div class="easyui-layout" fit="true">
        <div region="west" split="true" style="width:120px;">
            <ul id="tt"></ul>
        </div>
        <div region="center" border="false" border="true">
            <div id='newTT' class="easyui-tabs" fit="true">
                <div title="新闻管理" style="padding:10px;">
                    <table id="dg" style="height: 92%; width: 100%">
                        <input class="easyui-searchbox" data-options="prompt:'输入关键词进行搜索',searcher:doSearch" style="width:300px"></input>
                    </table>
                    <div id="dlg" class="easyui-dialog" title="编辑新闻" data-options="iconCls:'icon-edit',closed:true" style="width:400px;height:360px;padding:10px">

                        <form id="ff" method="post" >
                            <table cellpadding="5">
                                <tr>
                                    <td>id:</td>
                                    <td><input type="text" name="_id" ></input></td>
                                </tr>
                                <tr>
                                    <td>castId:</td>
                                    <td><input id='castId' type="text" name="castId" ></input></td>
                                </tr>
                                <tr>
                                    <td>标题:</td>
                                    <td><input class="easyui-textbox" type="text" name="title" data-options="required:true"></input></td>
                                </tr>
                                <tr>
                                    <td>内容:</td>
                                    <!--<td><input class="easyui-textbox" type="text" name="content" data-options="required:true"></input></td>-->
                                    <td><textarea name="content" class="textarea easyui"  data-options="required:true" rows="5" cols="22"></textarea></td>
                                </tr>
                                <tr>
                                    <td>发布时间:</td>
                                    <td><input id='date'  type="text" name="date" ></input></td>
                                </tr>
                            </table>
                        </form>
                        <div style="text-align:center;padding:5px">
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">提交</a>
                            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearForm()">清空</a>
                        </div>

                    </div>
                </div>
                <div title="查看更多"  data-options="iconCls:'icon-reload'"  >


                    <div class="easyui-panel" data-options="fit:true" title="Nested Panel" style="width:700px;height:200px;padding:10px;">
                        <div class="easyui-layout" data-options="fit:true">
                            <div data-options="region:'west',split:true" style="width:200px;padding:10px">

                                <div style=" font-size:20px;color: #4b72a4" id="newTitle"></div>
                                <div style=" color: firebrick" id="newTime"></div>
                                <div style="text-indent: 25px; margin-top: 40px"  id="newD"></div>
                            </div>
                            <div data-options="region:'east'" style="width:265px;padding:10px">
                                用户评论



                                <!--<div id="dlgm" class="easyui-dialog" title="用户评论" data-options="iconCls:'icon-edit',closed:true" style="width:400px;height:360px;padding:10px">-->

                                    <form id="ffm" method="post" >
                                        <table cellpadding="5">
                                            <tr>
                                                <td>newId:</td>
                                                <td><input id='newId' type="text" name="newId" ></input></td>
                                            </tr>
                                            <!--<tr>-->
                                                <!--<td>标题:</td>-->
                                                <!--<td><input class="easyui-textbox" type="text" name="title" data-options="required:true"></input></td>-->
                                            <!--</tr>-->
                                            <tr>
                                                <td>内容:</td>
                                                <!--<td><input class="easyui-textbox" type="text" name="content" data-options="required:true"></input></td>-->
                                                <td><textarea  name="comm"  class="xheditor-simple"  rows="5" cols="22"></textarea></td>
                                            </tr>
                                            <tr>
                                                <td>发布时间:</td>
                                                <td><input id='datem'  type="text" name="date" ></input></td>
                                            </tr>
                                        </table>
                                    </form>
                                    <div style="text-align:center;padding:5px">
                                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitFormCom()">提交</a>
                                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="clearFormCom()">清空</a>
                                    </div>

                                <!--</div>-->





                            </div>
                            <div data-options="region:'center'" style="padding:10px">
                                评论列表


                                <table id="dgm" style="height: 92%; width: 100%">
                                    <input class="easyui-searchbox" data-options="prompt:'Please Input Value',searcher:doSearch" style="width:300px"></input>
                                </table>




                            </div>
                        </div>
                    </div>





                    No contact data.
                </div>
            </div>



        </div>


    </div>
    </div>
</div>

</body>
</html>
<script>
    var rowNew;
	function doSearch(value){
		$('#dg').datagrid('load', {
			title: value,
			address: 'ho'
		});
	}
	function submitForm(){
		$('#ff').form('submit',{
			onSubmit:function(){
				if($(this).form('enableValidation').form('validate')) {
					var data=$('#ff').serializeObject();
					console.log(data._id&&data._id.trim().length>0);
					if (data._id && data._id.trim().length>0){
						$.ajax({
							method:'put',
							url:'http://localhost:3000/news/data/' + data._id,
							data:data
						}).done(function (res) {
							$('#dlg').dialog('close');
							$('#dg').datagrid('reload');
							$.messager.alert('提示信息','信息修改成功！');
						})
					}else {
						delete data._id;
//						ajax请求
						$.ajax({

							method:'post',
							url:'http://localhost:3000/news/data',
							data:data
						}).done(function (res) {
							$('#dlg').dialog('close');
							$('#dg').datagrid('reload');
							$.messager.alert('提示信息','信息添加成功！');
						})
					}
				}else {
					//表单验证错误
					$.messager.alert('提示信息','输入信息有误！');
				}
				console.log($(this).form('enableValidation').form('validate'));
				return $(this).form('enableValidation').form('validate');
			}
		});
	}
	function clearForm(){
		$('#ff').form('clear');
	}

	function clearFormCom(){
		$('#ffm').form('clear');
	}


	var toolbar = [{
		text:'Add',
		iconCls:'icon-add',
		handler:function(){
			$("#dlg").dialog('open')
		}
	},{
		text:'删除',
		iconCls:'icon-cancel',
		handler:function(){
			$.messager.confirm('Confirm', '确定删除这条用户信息吗？', function(r){
				if (r){
					// exit action;
					var gss=$('#dg').datagrid('getSelections');
					var ids=[];
					var gLength=gss.length;
					for(var i=0; i<gLength;i++ ){
						ids.push(gss[i]._id);
					}
					console.log(ids.toString());
					$.ajax({
						url:'http://localhost:3000/news/deletes',
						method:'post',
						data:{ids:ids.toString()}
					}).done(function (res) {
						if (res.status===200){
							$.messager.alert('提示信息','删除成功！');
						}else {
							$.messager.alert('提示信息','删除失败！');
						}
						$("#dg").datagrid("reload");
					})
				}
			});

		}
	},'-',{
		text:'返回',
		iconCls:'icon-back',
		handler:function(){
			$('#dg').datagrid('load', {
				name:"",
				address: 'ho'
			});
		}
	}];
	var toolbar1 = [
//		{
//		text:'Add',
//		iconCls:'icon-add',
//		handler:function(){
//			$("#dlg").dialog('open')
//		}
//	},
        {
		text:'删除',
		iconCls:'icon-cancel',
		handler:function(){
			$.messager.confirm('Confirm', '确定删除此信息吗？', function(r){
				if (r){
					// exit action;
					var gss=$('#dgm').datagrid('getSelections');
					var ids=[];
					var gLength=gss.length;
					for(var i=0; i<gLength;i++ ){
						ids.push(gss[i]._id);
					}
					console.log(ids.toString());
					$.ajax({
						url:'http://localhost:3000/comment/deletes',
						method:'post',
						data:{ids:ids.toString()}
					}).done(function (res) {
						if (res.status===200){
							$.messager.alert('提示信息','删除成功！');
						}else {
							$.messager.alert('提示信息','删除失败！');
						}
						$("#dgm").datagrid("reload");
					})
				}
			});

		}
	},'-',{
		text:'返回',
		iconCls:'icon-back',
		handler:function(){
//			$('#dg').datagrid('load', {
//				name:"",
//				address: 'ho'
//			});
			$("#newTT").tabs("select",0);
		}
	}];
	$('#dg').datagrid({
		toolbar:toolbar,
		pagination:true,
		method:'post',
//		onBeforeSelect:function(){
//			return false;
//		},
		url:'http://localhost:3000/news/list',
		columns:[[
			{field:'ck',checkbox:true},
			{field:'_id',title:'id',width:100},
			{field:'castId',title:'castId',width:100},
			{field:'title',title:'标题',width:250},
			{field:'date',title:'发布时间',width:100},
			{field:'num',title:'评论',width:25},
			{field:'opt',title:'操作',width:150,
				formatter:function (value,row,index) {
					return " <a name='dell'  href='javascript:void(0)' id='delete' onclick='del()'>删除</a>" +
                        " <a  name='more'  href='javascript:void(0)'  onclick='newsDetail("+JSON.stringify(row)+")' >查看更多<a/>"
				}}

		]],
		onLoadSuccess:function(data){
			$("a[name='dell']").linkbutton({text:'删除',plain:true,iconCls:'icon-no'});
			$("a[name='more']").linkbutton({text:'查看更多',plain:true,iconCls:'icon-more'});
		},

		onDblClickRow(index,row){
			editDate(row);
		}
	});


	$('#dgm').datagrid({
		toolbar:toolbar1,
		pagination:true,
		method:'post',
//		onBeforeSelect:function(){
//			return false;
//		},
		url:'http://localhost:3000/comment/list',
		columns:[[
			{field:'ck',checkbox:true},
//			{field:'_id',title:'id',width:100},
			{field:'newId',title:'newId',width:45},
			{field:'comm',title:'内容',width:250},
			{field:'date',title:'发布时间',width:100},
			{field:'opt',title:'操作',width:50,
				formatter:function (value,row,index) {
					return " <a name='delll'   href='javascript:void(0)' id='delete' onclick='delM()'>删除</a> "
				}}

		]],

		onLoadSuccess:function(data){
			$("a[name='delll']").linkbutton({text:'删除',plain:true,iconCls:'icon-no'});
		},

	});


	function editDate(row) {
		$('#dlg').dialog('open');
		$('#ff').form('load',row);
	}

	function newsDetail(row) {
		$("#newTT").tabs("select",1);
		$("#newTitle").text(row.title);
		$("#newTime").text(row.date);
		$("#newD").text(row.content);
		$('#dlgm').dialog('open');
		$('#newId').val(row._id);
        rowNew=row._id;
		console.log(rowNew);
		var d=new Date();
		var m1="/";
		var m2=":";
		var time=d.getFullYear()+m1+parseInt(d.getMonth()+1)+m1+d.getDate()+" "+d.getHours()+m2+d.getMinutes();
		console.log(time);
		$('#datem').val(time);
		$('#dgm').datagrid('load', {
			newId: row._id,
			address: 'ho'
		});
	}

	function submitFormCom(){
		$('#ffm').form('submit',{
			onSubmit:function(){
				if($(this).form('enableValidation').form('validate')) {
					var data=$('#ffm').serializeObject();
					console.log(data._id&&data._id.trim().length>0);
					if (data._id && data._id.trim().length>0){
						$.ajax({
							method:'put',
							url:'http://localhost:3000/comment/data/' + data._id,
							data:data
						}).done(function (res) {
//							$('#dlg').dialog('close');
//							$('#dg').datagrid('reload');
							$.messager.alert('提示信息','信息修改成功！');
						})
					}else {
						delete data._id;
//						ajax请求
						$.ajax({

							method:'post',
							url:'http://localhost:3000/comment/data',
							data:data
						}).done(function (res) {
//							$('#dlg').dialog('close');
							$('#dgm').datagrid('reload');
							$.messager.alert('提示信息','信息添加成功！');
						})
						//查询数据库中表里的评论数

						$.ajax({

							method:'get',
							url:'http://localhost:3000/news/data/'+rowNew,
						}).done(function (res) {
//							var Num=res.num;
							console.log(res.num);
							var Num=res.num+1;
							$.ajax({
                                method:'put',
								url:'http://localhost:3000/news/data/'+rowNew,
                                data:{num:Num}
                            }).done(function (res) {
								console.log(res);
								$("#dg").datagrid("reload");

							})

//

						})

					}
				}else {
					//表单验证错误
					$.messager.alert('提示信息','输入信息有误！');
				}
				console.log($(this).form('enableValidation').form('validate'));
				return $(this).form('enableValidation').form('validate');


			}
		});
	}


	function del(){
		$.messager.confirm('Confirm', '确定删除这条用户信息吗？', function(r){
			if (r){
				// exit action;
				var gss=$('#dg').datagrid('getSelections');
				var ids=[];
				var gLength=gss.length;
				for(var i=0; i<gLength;i++ ){
					ids.push(gss[i]._id);
				}
				console.log(ids.toString());
				$.ajax({
					url:'http://localhost:3000/news/deletes',
					method:'post',
					data:{ids:ids.toString()}
				}).done(function (res) {
					if (res.status===200){
						$.messager.alert('提示信息','删除成功！');
					}else {
						$.messager.alert('提示信息','删除失败！');
					}
					$("#dg").datagrid("reload");
				})
			}
		});
	}
	function delM(){
		$.messager.confirm('Confirm', '确定删除这条用户信息吗？', function(r){
			if (r){
				// exit action;
				var gss=$('#dgm').datagrid('getSelections');
				var ids=[];
				var gLength=gss.length;
				for(var i=0; i<gLength;i++ ){
					ids.push(gss[i]._id);
				}
				console.log(ids.toString());
				$.ajax({
					url:'http://localhost:3000/comment/deletes',
					method:'post',
					data:{ids:ids.toString()}
				}).done(function (res) {
					if (res.status===200){
						$.messager.alert('提示信息','删除成功！');




						$.ajax({

							method:'get',
							url:'http://localhost:3000/news/data/'+rowNew,
						}).done(function (res) {
//							var Num=res.num;
							console.log(res.num);
							var Num=res.num-1;
							$.ajax({
								method:'put',
								url:'http://localhost:3000/news/data/'+rowNew,
								data:{num:Num}
							}).done(function (res) {
								console.log(res);
								$("#dg").datagrid("reload");

							})


//







						})










					}else {
						$.messager.alert('提示信息','删除失败！');
					}
					$("#dgm").datagrid("reload");
				})
			}
		});
	}

	$('#tt').tree({
		method:'get',
		url:'http://localhost:3000/cate/list',
		onClick(node){
			$("#newTT").tabs("select",0);
			var d=new Date();
			var m1="/";
			var m2=":";
			var time=d.getFullYear()+m1+parseInt(d.getMonth()+1)+m1+d.getDate()+" "+d.getHours()+m2+d.getMinutes();
			console.log(time);
			$('#date').val(time);


			console.log(node.text);
			console.log(node._id);
			$('#castId').val(node._id);
			$('#dg').datagrid('load', {
				castId: node._id,
				address: 'ho'
			});
			$('#parentId').val(node._id);

		}
	});
</script>